package mn.idax.exchange.otc.util;

import com.alibaba.fastjson.JSON;
import mn.idax.exchange.common.SpringUtils;
import mn.idax.exchange.common.bean.ResponseData;

import java.text.MessageFormat;
import java.util.Locale;

/**
 * @Auther: dison
 * @Date: 2018/9/29 09:50
 * @Description:
 */
public class I18nUtil {

    public static String getMessage(String code , String lang , Object... dynamicParam){
        String messageCode= "message." + code;
        Locale locale = null;
        if(lang==null){
            lang = "zh_cn";
        }
        String langArr[] = lang.replace("-","_").split("_");
//        if (langArr != null && langArr.length == 1){
//            langArr = lang.split("_");
//        }
        if (langArr != null && langArr.length > 1){
            locale = new Locale(langArr[0], langArr[1]);
        }else{
            locale = getDefaultCountry(langArr[0]);
        }
        String messageBody = SpringUtils.getMessage(messageCode,locale , dynamicParam);

        return messageBody;
    }

    public static Locale getDefaultCountry(String language){
        if ("zh".equalsIgnoreCase(language)){
            return new Locale(language , "cn");
        }
        return new Locale(language , "");
    }


    public static Object replaceLangeage(String returnJson,String...obj) {
        if (returnJson == null) {
            return returnJson;
        } else {
            ResponseData responseData = null;
//            int code = false;
            String message = null;

            int code;
            try {
                responseData = (ResponseData) JSON.parseObject(returnJson, ResponseData.class);
                if (responseData == null) {
                    return returnJson;
                }

                code = responseData.getSuccess();
                String messageCode = "message." + String.valueOf(code);
//                MessageFormat.fo
                message = SpringUtils.getMessage(messageCode, obj);
            } catch (Exception var5) {
                return returnJson;
            }

            return ResponseData.returnResponse(code, message, responseData.getData());
        }
    }
}
